library(egg)
library(tidyverse)

load("eng_plot_coords.RDS")
load("bio_plot_coords.RDS")

tmp_df <- data.frame(means = unlist(tmp_final$means),
                     labels = tmp_final$labels,
                     topics = tmp_final$topics)

tmp_df <- tmp_df[order(-tmp_df$means),]
tmp_df$Gender <- ifelse(tmp_df$means > 0, "F","M")

tmp_df_bio <- data.frame(means = unlist(tmp_bio$means),
                     labels = tmp_bio$labels,
                     topics = tmp_bio$topics)

tmp_df_bio <- tmp_df_bio[order(-tmp_df_bio$means),]
tmp_df_bio$Gender <- ifelse(tmp_df_bio$means > 0, "F","M")

tmp_df_bio$Gender <- ifelse(tmp_df_bio$means > 0, "F","M")

pdf("bio_eng_side.pdf", width = 12, height = 20)
#width = 14, height = 16)
bio_plot <- ggplot(tmp_df_bio, aes(x=means,y=reorder(labels,means), color = Gender,
                                   label = labels)) +
  geom_point(size = 3.5) +
  
  scale_x_continuous(breaks=seq(-0.0175, 0.0175, 0.005), limits = c(-0.0175, 0.0175)) +
  geom_vline(xintercept = 0, linetype="dashed", 
             color = "black", size=.5) +
  geom_text(aes(label = labels), data=subset(tmp_df_bio, means >0), size=5,
            hjust = -.3, vjust = .1, color = "black", show.legend = FALSE)+
  geom_text(aes(label = labels), data=subset(tmp_df_bio, means <0), size=5,
            hjust = 1.2, vjust = .1, color = "black", show.legend = FALSE)+
  ylab("") +
  xlab( "\n      More Male           ...           More Female\n\n       Difference in Topic Proportions\n")+
  labs(title = "Biology\n") +
  coord_cartesian(clip = "off")+
  theme_minimal() +
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.title.x = element_text(size = 14),
        axis.text.x  = element_text(size = 12),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5,size = 15)
  )


eng_plot <- ggplot(tmp_df, aes(x=means,y=reorder(labels,means), color = Gender,
                               label = labels)) +
  geom_point(size= 3.5) +
  scale_x_continuous(breaks=seq(-0.0175, 0.0175, 0.005), limits = c(-0.0175, 0.0175)) +
  geom_vline(xintercept = 0, linetype="dashed", 
             color = "black", size=.5) +
  geom_text(aes(label = labels), data=subset(tmp_df, means >0), size=5,
            hjust = -.2, vjust = .5, color = "black", show.legend = FALSE)+
  geom_text(aes(label = labels), data=subset(tmp_df, means <0), size=5,
            hjust = 1.2, vjust = .1, color = "black", show.legend = FALSE)+
  ylab("") +
  xlab( "\n      More Male           ...           More Female\n\n       Difference in Topic Proportions\n")+
  labs(title = "Engineering") +
  coord_cartesian(clip = "off")+
  theme_minimal() +
  guides(color = guide_legend(override.aes = list(size=6))) +
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.title.x = element_text(size = 14),
        axis.text.x  = element_text(size = 12),
        plot.title = element_text(hjust = 0.5,size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 14),
        legend.position = "bottom"
  )

egg::ggarrange(bio_plot, eng_plot, ncol = 1)

dev.off()
